﻿(function ($) {

    //TODO: consider methods of automatically using groups with selectors

    //pushes c on to the array a if c is a jquery object; c=context, a=arguments
    var ap = function (c, a) {
        if (c && c.jquery) (a = $.makeArray(a)).push(c);
        return a;
    }

    //calls the window.console.<f> function if it exists
    var wc = function (f, a) {
        var c = window.console, l = c && c[f];
        if (l) return l.apply(c, a);
    }

    //this can be overridden to change the fallback action when a log method doesn't exist
    $.log = function () { return wc('log', arguments); }

    //calls the window.console.<f> function if it exists, automatically appending <ct> to the arguments if it is a jquery object (for $.fn functions), or falls back to $.log
    var wcl = function (f, ct, ar) {
        var a = ap(ct, ar),
            c = window.console, l = c && c[f] || $.log;
        return l.apply(c, a);
    }

    //element and static functions
    var fs = ["info", "warn", "error", "debug", "dir", "dirxml"];
    for (var idx in fs) {
        (function () {
            var f = fs[idx];
            $.fn[f] = $[f] = function () {
                return wcl(f, this, arguments);
            };
        })();
    }

    //static only functions
    fs = ["trace", "group", "groupEnd", "time", "timeEnd", "profile", "profileEnd"];
    for (idx in fs) {
        (function () {
            var f = fs[idx];
            $[f] = function () { return wc(f, arguments); };
        })();
    }

    //    var f = {
    //        info: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.info || $.log;
    //            l.call(args);
    //        },
    //        warn: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.warn || $.log;
    //            l.call(args);
    //        },
    //        error: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.error || $.log;
    //            l.call(args);
    //        },
    //        debug: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.debug || $.log;
    //            l.call(args);
    //        },
    //        dir: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.dir || $.log;
    //            l.call(args);
    //        },
    //        dirxml: function () {
    //            var args = argPush.apply(this, arguments);
    //            var w = window, c = w.console, l = c && c.dirxml || $.log;
    //            l.call(args);
    //        }
    //    };

    //    $.trace = function () {
    //        var w = window, c = w.console, l = c && c.trace;
    //        if (l) l.call(arguments);
    //    };

    //    $.group = function () {
    //        var w = window, c = w.console, l = c && c.group;
    //        if (l) l.call(arguments);
    //    }

    //    $.groupEnd = function () {
    //        var w = window, c = w.console, l = c && c.groupEnd;
    //        if (l) l.call(arguments);
    //    }

    //    $.time = function () {
    //        var w = window, c = w.console, l = c && c.time;
    //        if (l) l.call(arguments);
    //    }

    //    $.timeEnd = function () {
    //        var w = window, c = w.console, l = c && c.timeEnd;
    //        if (l) l.call(arguments);
    //    }

    //    $.profile = function () {
    //        var w = window, c = w.console, l = c && c.profile;
    //        if (l) l.call(arguments);
    //    }

    //    $.profileEnd = function () {
    //        var w = window, c = w.console, l = c && c.profileEnd;
    //        if (l) l.call(arguments);
    //    }

})(jQuery);